package cn.tedu.route;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class RouteProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂对象
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("192.168.8.100");
        cf.setPort(5672);   //默认端口号，可以省略不写
        cf.setUsername("test");
        cf.setPassword("test");
        cf.setVirtualHost("/");  //如果不指定虚拟主机，默认是"/",可以省略不写
        //2.通过连接工厂对象创建tcp连接对象，通过tcp连接对象创建channel
        Connection nc = cf.newConnection();
        Channel cc = nc.createChannel();
        //3.通过channel创建交换机
        //第一个参数：交换机的名称
        //第二个参数：交换机的类型
        cc.exchangeDeclare("routeMode.logs", "direct");
        //4.发送数据
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入消息数据:");
            String msg = scanner.nextLine();
            System.out.print("请输入路由键:");
            String routingKey = scanner.nextLine();
            if (msg.equals("quit"))
                nc.close();
            //第一个参数：交换机的名称
            //第二个参数：路由键
            cc.basicPublish("routeMode.logs", routingKey, null, msg.getBytes(StandardCharsets.UTF_8));
        }
    }
}
